package listener;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;

@WebListener
public class UserCountListener implements  HttpSessionListener {

    public UserCountListener() {
    }


    @Override
    public void sessionCreated(HttpSessionEvent se) {
        /* 当Session 创建出来的时候，执行这个方法+1 */
        //在线人数数据
        ServletContext ctx = se.getSession().getServletContext();
        Integer count = (Integer) ctx.getAttribute("OnlineCount");
        if (count == null){
            count = 0;

        }
        count++;
        ctx.setAttribute("OnlineCount",count);
    }

    @Override
    public void sessionDestroyed(HttpSessionEvent se) {
        /* 当Session 销毁出来的时候，执行这个方法-1 */
        ServletContext ctx = se.getSession().getServletContext();
        Integer count = (Integer) ctx.getAttribute("OnlineCount");
        if (count == null){
            count = 0;

        }
        count--;
        ctx.setAttribute("OnlineCount",count);
    }



}
